package com.example.teachdemo4;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.net.URI;

public class SurfaceViewActivity extends AppCompatActivity {

    SurfaceView surfaceView;
    MediaPlayer mediaPlayer;
    SurfaceHolder holder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_surface_view);
        surfaceView = findViewById(R.id.surfaceView);

        holder = surfaceView.getHolder();

        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {

                mediaPlayer = new MediaPlayer();
                mediaPlayer.setDisplay(holder);
                try {
                    mediaPlayer.setDataSource(getApplicationContext(),
                            Uri.parse("android.resource://com.example.teachdemo4/" + R.raw.qhc));
                    mediaPlayer.prepare();

                }catch (Exception e){

                }
                //mediaPlayer.start();

                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {
                        mediaPlayer.start();
                    }
                });

            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {

            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {

            }
        });

    }
}
